#include <QtGui/QApplication>
#include <QTextCodec>
#include "AutoTest.h"

#include <QtGui>

#include "CalendarModel.h"
#include "CalendarView.h"
#include "SqlInterface.h"
#include <QSqlQuery>
#include <QDebug>

#include <QDeclarativeContext>
#include <QDeclarativeView>

#include <QFile>
#include <QTextStream>

#ifdef DEBUG_LOG
void customMessageHandler(QtMsgType type, const char *msg)
{
    QString txt;
    switch (type) {
        case QtDebugMsg:
            txt = QString("Debug: %1").arg(msg);
            break;

        case QtWarningMsg:
            txt = QString("Warning: %1").arg(msg);
            break;
        case QtCriticalMsg:
            txt = QString("Critical: %1").arg(msg);
            break;
        case QtFatalMsg:
            txt = QString("Fatal: %1").arg(msg);
            abort();
    }

    QFile outFile(DEBUG_LOG);
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
    ts << txt << endl;
}
#endif

int main(int argc, char *argv[])
{
    QTextCodec *codec = QTextCodec::codecForName("UTF8");
    QTextCodec::setCodecForTr(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForLocale(codec);

#ifdef DEBUG_LOG
    QFile cleanFile(DEBUG_LOG);
    cleanFile.open(QIODevice::WriteOnly);
    cleanFile.close();
    qInstallMsgHandler(customMessageHandler);
#endif

    // прогон тестов
    AutoTest::run(argc, argv);

    // запуск приложения
    QApplication app(argc, argv);

    SqlInterface::addConnectDBMemory();
    SqlInterface::createDB();
    SqlInterface::fillDB();

    CalendarModel *model = new CalendarModel();
//    QDate date = QDate::currentDate();
    QDate date(2011, 7, 7);
    model->showMonth(date.year(), date.month());

    QVariant data = model->index(2, 4).data(CalendarModel::EVENT);
    if (!data.isValid()) {
        qDebug() << "! valid";
    }
    EventList_t eventList = data.value<EventList_t>();
    if (eventList.empty()) {
        qDebug() << "empty";
    }

    CalendarView view;
    view.setModel(model);
    view.resize(600, 600);
    view.show();

//    QPushButton prevButton;
//    QPushButton nextButton;
//    QWidget navigator;
//    QHBoxLayout layout;
//    QSpacerItem spacer(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);

//    qDebug() << "createEditor";
//    prevButton.setMaximumWidth(10);
//    prevButton.setText("<");
//    nextButton.setMaximumWidth(10);
//    nextButton.setText(">");
//    nextButton.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

//    layout.setContentsMargins(0, 0, 0, 0);
//    layout.addWidget(&prevButton);
//    layout.addSpacerItem(&spacer);
//    layout.addWidget(&nextButton);
//    navigator.setLayout(&layout);

//    navigator.show();

//    QDeclarativeView *qmlView = new QDeclarativeView;
//    qmlView->rootContext()->setContextProperty("calendarModel", model);
//    qmlView->setSource(QUrl::fromLocalFile("../qml/main.qml"));
//    qmlView->show();

    return app.exec();
//    return 0;
}
